<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="qq" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hShowAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          :data="list"
          default-expand-all
        >
          <template v-slot="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height:40px;width:100%">
              <el-col :span="20">
                <span>{{ data.name }}</span>
                <!-- <span>{{ heade(scope) }}</span> -->
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hShowAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hShowEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
      :title="isEdit ? '编辑' : '添加'"
      :visible.sync="showDialog"
      width="50%"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
    >
      <add-or-edit
        v-if="showDialog"
        :id="curId"
        :is-edit="isEdit"
        :origin-list="originList"
        @success="hSuccess"
      />
    </el-dialog>

    <el-dialog
      :title="isEdit ? '编辑' : '添加'"
      :visible.sync="showDialogEdit"
      width="50%"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
    >
      <add-or-edit
        v-if="showDialogEdit"
        :id="curId"
        :is-edit="isEdit"
        :origin-list="originList"
        @success="hSuccess"
      />
    </el-dialog>
  </div>
</template>
<script>
import addOrEdit from './deptDialog.vue'
import { deleteDepartmentById, getDepartmentList } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      // 依赖一份树形数据
      // list: [{
      //   name: '财务部',
      //   manager: '刘备',
      //   children: [
      //     {
      //       name: '财务核算部',
      //       manager: '张飞'
      //     },
      //     {
      //       name: '税务核算部',
      //       manager: '关羽'
      //     }
      //   ]
      // }]
      list: [],
      showDialog: false,
      curId: '',
      // 是否处于编辑状态
      isEdit: false,
      showDialogEdit: false,
      originList: []
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async  getDepartments() {
      const res = await getDepartmentList()
      // 平面结果，将其所需的数据筛选循环，传入自定义数组中，传给子组件
      this.originList = res.data.depts.map(item => {
        return { id: item.id, pid: item.pid, code: item.code, name: item.name }
      })
      this.list = tranListToTreeData(res.data.depts)
      this.list.shift()

      // console.log(res)
    },
    hShowAdd(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = false
    },
    hShowEdit(id) {
      this.showDialogEdit = true
      this.curId = id
      this.isEdit = true
    },
    hSuccess() {
      this.showDialog = false
      this.showDialogEdit = false
      this.getDepartments()
    },
    hDel(id) {
      this.$confirm('要退出吗,嘤嘤嘤', '提示', {
        type: 'warning'
      })
        .then(async() => {
          try {
            const res = await deleteDepartmentById(id)
            this.$message.success(res.message)
            this.getDepartments()
          } catch (e) {
            this.$message.error(e.message)
          }
        })
        .catch(() => {})
    }

  }
}
</script>
